//
//  YSOriginalView.swift
//  新浪浪
//
//  Created by mac on 16/3/2.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

let CellMargin = 10


class YSOriginalView: UIView {
    
 var bottomConstarint: Constraint?
    
 var statusViewModel: YSStatusViewModel?{
    
        didSet {
            nameLabel.text = statusViewModel?.statusModel?.user?.screen_name
            textLabel.text = statusViewModel?.statusModel?.text
            timeLabel.text = statusViewModel?.dealCreateDate(statusViewModel?.statusModel?.created_at)   // statusViewModel?.statusModel?.created_at
            sourceLabel.text = statusViewModel?.dealSource(statusViewModel?.statusModel?.source)
            
            
            veriftyImageView.image = statusViewModel?.verifity_image
            
//            levelImageView.image = statusViewModel?.level_image
            
            bottomConstarint?.uninstall()
            
            if let pic_url = statusViewModel?.statusModel?.pic_urls where pic_url.count > 0{
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                   bottomConstarint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                //传值
//                pictureView.pictureViewModel = statusViewModel
                //需要把model转换成ViewModel
                //定义一个临时数组进行数据拼接
                var result = [YSStatusViewModel]()
                for model in pic_url{
                    let viewModel = YSStatusViewModel()
                    viewModel.picture = model
                    result.append(viewModel)
                }
                pictureView.pictureList = result
                
            }else {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstarint = make.bottom.equalTo(textLabel.snp_bottom).constraint
                })
            }
            
        }
    }
    
    /*
    原创微博 --
    头像
    认证图片
    微博名字
    会员等级
    发表时间
    发表来源
    发表内容
    */
    
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        setUpUI()
    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
let Magin = 10
    private func setUpUI(){
        addSubview(photoImageView)
        addSubview(veriftyImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(textLabel)
        //添加图片
        addSubview(pictureView)
        
        //头像约束 -- 左-10 上边 10 宽 40 高 40
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(Magin)
            make.top.equalTo(self.snp_top).offset(Magin)
            
            make.size.equalTo(CGSizeMake(40, 40))
        }
        //认证图片的约束 -- 中心点
        veriftyImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        // 名字 --左边 上边
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(Magin)
            make.top.equalTo(photoImageView.snp_top)
        }
        
        //等级 -- 左边 中心线
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(Magin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        
        // 时间的约束 -- 左 10,上边 5
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(Magin)
            make.top.equalTo(nameLabel.snp_bottom).offset(5)
        }
        
        // 来源 -- 左边 10 ,中心线
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(Magin)
            make.centerY.equalTo(timeLabel.snp_centerY)
        }
        
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(photoImageView.snp_bottom).offset(Magin)
            make.left.equalTo(self.snp_left).offset(Magin)
            
//            make.bottom.equalTo(self.snp_bottom)
            make.width.equalTo(UIScreen.mainScreen().bounds.width - 2 * CGFloat(Magin))
            
            
            
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(Magin)
            make.top.equalTo(textLabel.snp_bottom).offset(Magin)
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            bottomConstarint = make.bottom.equalTo(pictureView.snp_bottom).offset(Magin).constraint
        }
        
    }
    
    
    /*
                            原创微博 --
    头像
    认证图片
    微博名字
    会员等级
    发表时间
    发表来源
    发表内容
    */
    private lazy var photoImageView:UIImageView = {
        let ima = UIImageView()
        
        ima.image = UIImage (named: "avatar_default_big")
        
        
        return ima
        
    
    }()

//       认证图片
    private lazy var veriftyImageView:UIImageView = {
        let ima = UIImageView()
        
        ima.image = UIImage (named: "avatar_vip")
        
        return ima
    }()
    
//    微博名字
//    会员等级
//    发表时间
//    发表来源
//    发表内容
    private lazy var nameLabel : UILabel = {
        let lab = UILabel()
        
        lab.text = "微博名字"
        
        lab.font = UIFont.systemFontOfSize(15)
        
        return lab
    }()
    
    //    会员等级
    //    发表时间
    //    发表来源
    //    发表内容
    private lazy var levelImageView: UIImageView = {
        let ima = UIImageView()
        
        ima.image = UIImage (named: "common_icon_membership_level1")
        
        return ima
    }()
    
    //    发表时间
    //    发表来源
    //    发表内容
    
    private lazy var timeLabel: UILabel = {
        let lab = UILabel()
        
        lab.text = "发表时间"
        lab.textColor = UIColor .orangeColor()
        lab.font = UIFont.systemFontOfSize(13)
        
        return lab
        }()
    
    //    发表来源
   
    private lazy var sourceLabel:UILabel = {
        let lab = UILabel()
        
       lab.text = "微博来源"
        lab.font = UIFont.systemFontOfSize(13)
        return lab
    }()
    
     //    发表内容
    private lazy var textLabel:UILabel = {
        let lab = UILabel()
        
        lab.text = "微博内容"
        lab.font = UIFont.systemFontOfSize(15)
        lab.numberOfLines = 0
        
        return lab
        }()
    
    
    private lazy var pictureView : YSPicture = YSPicture()
}














